//
//  HWGameLiveChatCell.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/10/27.
//

import UIKit

class HWGameLiveChatCell: UITableViewCell {
    var managerImageView:UIImageView!
    var levelLabel:UILabel!
    var levelView:UIView!
    var contentLabel:UILabel!
    var model:HWGameLiveChatModel?{
        didSet{
//            self.contentLabel.text = model?.text
            let attriString = NSMutableAttributedString.init(string: "\(model?.userName ?? "")：\(model?.text ?? "")")
            
            attriString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#8D9AB1"), range: NSMakeRange(0, (model?.userName ?? "").count + 1))
            attriString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#333333"), range: NSMakeRange((model?.userName ?? "").count + 1, attriString.length-((model?.userName ?? "").count + 1)))
            
//            let markattch = NSTextAttachment() //定义一个attachment
//            markattch.image = HWUniversal.convertCreateImageWithUIView(v: managerImageView)//初始化图片
//            markattch.bounds = CGRect(x: 0, y: -3, width: 14, height: 14) //初始化图片的 bounds
//            let markattchStr = NSAttributedString(attachment: markattch) // 将attachment  加入富文本中
//            attriString.insert(markattchStr, at: 0)// 将markattchStr  加入原有文字的某个位置
//
//            let markattch1 = NSTextAttachment()
//            markattch1.image = HWUniversal.convertCreateImageWithUIView(v: levelView)
//            markattch1.bounds = CGRect(x: 0, y: -3, width: 49, height: 14)
//            let markattchStr1 = NSAttributedString(attachment: markattch1)
//            attriString.insert(markattchStr1, at: 1)
            
            let style = NSMutableParagraphStyle()
            style.lineSpacing = 5
            style.lineBreakMode = NSLineBreakMode.byWordWrapping
            attriString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, attriString.length))
            
            contentLabel.attributedText = attriString
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWGameLiveChatCell) {
        let reUse:String = "HWGameLiveChatCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameLiveChatCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameLiveChatCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        managerImageView = UIImageView()
        managerImageView.backgroundColor = .red
        managerImageView.frame.size = CGSize(width: 14, height: 14)
        
        levelLabel = HWLabel.createLabel(title: "LV12", color: "#FFFFFF", font: 8)
        levelLabel.backgroundColor = .colorWithHexString(hexString: "#BA3FFD")
        levelLabel.layer.cornerRadius = 2
        levelLabel.layer.masksToBounds = true
        levelLabel.textAlignment = .center
        levelLabel.frame = CGRect(x: 5, y: 0, width: 39, height: 14)
        
        levelView = UIView()
        levelView.backgroundColor = .white
        levelView.frame = CGRect(x: 0, y: 0, width: 49, height: 14)
        levelView.addSubview(levelLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 12*widthScale)
        contentView.addSubview(contentLabel)
        let attriString = NSMutableAttributedString.init(string: " 系统消息：提倡传播正能量内容，严格抵制低俗色情，一经发现永久封号。")
        
        attriString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#8D9AB1"), range: NSMakeRange(0, 5))
        attriString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.colorWithHexString(hexString: "#333333"), range: NSMakeRange(5, attriString.length-5))
        
        let markattch = NSTextAttachment() //定义一个attachment
        markattch.image = HWUniversal.convertCreateImageWithUIView(v: managerImageView)//初始化图片
        markattch.bounds = CGRect(x: 0, y: -3, width: 14, height: 14) //初始化图片的 bounds
        let markattchStr = NSAttributedString(attachment: markattch) // 将attachment  加入富文本中
        attriString.insert(markattchStr, at: 0)// 将markattchStr  加入原有文字的某个位置
        
        let markattch1 = NSTextAttachment()
        markattch1.image = HWUniversal.convertCreateImageWithUIView(v: levelView)
        markattch1.bounds = CGRect(x: 0, y: -3, width: 49, height: 14)
        let markattchStr1 = NSAttributedString(attachment: markattch1)
        attriString.insert(markattchStr1, at: 1)
        
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 5
        style.lineBreakMode = NSLineBreakMode.byWordWrapping
        attriString.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSMakeRange(0, attriString.length))
        
        contentLabel.attributedText = attriString
    }
    
    func initFrame() -> Void {
//        managerImageView.snp.makeConstraints { make in
//            make.size.equalTo(CGSize(width: 30, height: 30))
//        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(21*widthScale)
            make.right.equalTo(-21*widthScale)
            make.top.equalTo(5*widthScale)
            make.bottom.equalTo(-5*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
